# main.py

import os 
import sys
from PySide6.QtCore import QUrl, QSettings, QTimer
from PySide6.QtQml import QQmlApplicationEngine,qmlRegisterType
from PySide6.QtWidgets import QApplication
from PySide6.QtGui import QIcon, QWindow
from PySide6.QtQuick import QQuickWindow
from PySide6.QtCore import QObject,Property,Signal, Slot,Qt
from Qml.filter import EdgeResizeEventFilter
from Qml.theme import WQTheme
from ViewModel.Counter import CounterViewModel

 
from ctypes import cdll, c_void_p, c_bool, c_char_p

# def get_nswindow(window:QWindow):
#     """
#     利用 QWindow.winId() 返回的 NSView 指针，通过 Objective-C 的
#     -[NSView window] 方法获取 NSWindow 指针。
#     """
#     appKit = cdll.LoadLibrary("/System/Library/Frameworks/AppKit.framework/AppKit")
#     nsview = c_void_p(int(window.winId()))
#     # 设置 objc_msgSend 原型，返回 NSWindow 指针
#     objc_msgSend = appKit.objc_msgSend
#     objc_msgSend.restype = c_void_p
#     objc_msgSend.argtypes = [c_void_p, c_void_p]
#     sel_registerName = appKit.sel_registerName
#     sel_registerName.restype = c_void_p
#     sel_registerName.argtypes = [c_char_p]
#     sel_window = sel_registerName(b"window")
#     nswindow = objc_msgSend(nsview, sel_window)
#     return nswindow
# def enableMouseMoveForInactiveWindow(window):
#     if sys.platform == "darwin":
#         appKit = cdll.LoadLibrary("/System/Library/Frameworks/AppKit.framework/AppKit")
#         nswindow_ptr = get_nswindow(window)
#         nswindow = c_void_p(int(nswindow_ptr))
#         objc_msgSend = appKit.objc_msgSend
#         objc_msgSend.restype = None
#         objc_msgSend.argtypes = [c_void_p, c_void_p, c_bool]
#         sel_registerName = appKit.sel_registerName
#         sel_registerName.restype = c_void_p
#         sel_registerName.argtypes = [c_char_p]
#         sel = sel_registerName(b"setAcceptsMouseMovedEvents:")
#         objc_msgSend(nswindow, sel, c_bool(True))
 
if __name__ == "__main__":
    # 设置默认主题为Basic
    os.environ["QT_QUICK_CONTROLS_STYLE"] = "Fusion"
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon(':/Assert/images/logo.png'))
    # 获取当前调色板
    # 判断系统主题模式
    cs = app.styleHints().colorScheme()
    print(f'cs: {cs}')
    #qmlRegisterType(CounterViewModel, "Demo", 1, 0, "CounterViewModel")
    engine = QQmlApplicationEngine()
    # 创建 ViewModel 并将模型传递给它
    root = CounterViewModel()
    # 将 ViewModel 和 Model 注册到 QML 中
    theme = WQTheme()
    engine.rootContext().setContextProperty("WQTheme", theme)
    engine.rootContext().setContextProperty("app", root)
    engine.rootContext().setContextProperty("home", root.home) 
    # 加载 QML 文件
    #engine.load(QUrl("Qml/demo/tab.qml"))
    engine.load(QUrl("MainWindow.qml"))
    if not engine.rootObjects():
        sys.exit(-1)
    qmlWindow = engine.rootObjects()[0]
    # if sys.platform == 'darwin':
    #    enableMouseMoveForInactiveWindow2(qmlWindow)
    
    if sys.platform == 'win32':
        filter = EdgeResizeEventFilter(qmlWindow)
        qmlWindow.installEventFilter(filter)
     # 延迟调用，确保窗口已完全初始化
    sys.exit(app.exec())

